<?php
namespace app\index\controller;
use app\model\Product as ProductModel;

use think\Db;


class Product extends Base
{   
    protected $noCheckToken = ['index','read'];
    
    // 列表
    public function index(){
        $map_params = [
           ['key'=>'category_id','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        if (isSearchParam('keyword_search')) {
            $map[] = ['name','like','%'.input('keyword_search').'%'];
        }
        $map[] = ['status','=',1];
        $model = new ProductModel();

        $order_field = input('order_field','id');
        $order_type = input('order_type','desc');

        $list = $model->where($map)->order($order_field,$order_type)->select()->toarray();
        foreach ($list as $k => $vo) {
            if (empty($vo['tags'])) {
                $list[$k]['tags'] = [];
            }else{
                $list[$k]['tags'] = explode(',',$vo['tags']);
            }

            $list[$k]['group_num'] = $model->where('group',$vo['group'])->count();

            $collect = Db::name('collect')->where('uuid',$vo['collect'])->find();
            $list[$k]['doc_url'] = $collect['doc_url'];
            $list[$k]['collect_remark'] = $collect['remark'];
        }

        return apiResult('2000','获取成功',$list);
    }

    // 详情
    public function read($id){
        $model = new ProductModel();
        $data = $model->where('id',$id)->find();
        if (empty($data['tags'])) {
            $data['tags'] = [];
        }else{
            $data['tags'] = explode(',',$data['tags']);
        }

        $data['groups'] = $model->field(['id','name'])->where('group',$data['group'])->select();

        $collect = Db::name('collect')->where('uuid',$data['collect'])->find();
        $data['doc_url'] = $collect['doc_url'];
        $data['collect_remark'] = $collect['remark'];

        // $contents = [
        //     ['title'=>'模型介绍','type'=>'text','content'=>"专为 Agentic Coding 设计，全面覆盖编程任务与场景，通过大规模智能体强化学习，实现智能行为涌现，在代码编写性能上显著超越同类模型。"],
        //     ['title'=>'模型能力','type'=>'grid','content'=>"输入类型:文字\n输出类型:文字\nFunction Call:√\n模型体验:-"],
        //     ['title'=>'模型特点','type'=>'li','content'=>"坚实工程基础：通过'Mid-Training '注入真实世界的编程知识、工具调用及多轮交互能力。\n全面场景覆盖：精通功能实现、缺陷修复等8大核心开发任务与8大主流编程场景。"],
        // ];
        // dump(json_encode($contents,JSON_UNESCAPED_UNICODE));die;

        $contents = json_decode($data['content'],true);
        foreach ($contents as $k => $vo) {
            if ($vo['type'] == 'grid') {
                $content = explode("\n",$vo['content']);
                foreach ($content as $c_k => $c_v) {
                    $content[$c_k] = explode(':',$c_v);
                }
            }elseif($vo['type'] == 'li'){
                $content = explode("\n",$vo['content']);
            }else{
                $content = $vo['content'];
            }

            $contents[$k]['content'] = $content;
        }
        $data['content'] = $contents;

        return apiResult('2000','获取成功',$data);
    }

   


}
